﻿using MediatR;
using SlaughterItem.Common.Domains.Setup;
using SlaughterItem.Common.Domains.Warehouse;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.Scale
{
    public class WeighingSheetsShowHandler : IRequestHandler<WeighingSheetsShowCommand, Result>
    {
        IBaseRepositoryGet<DepotType> DepotTypeRep;
        IBaseRepositoryGet<WeighingSheets> WeighingSheetsRep;

        public WeighingSheetsShowHandler(IBaseRepositoryGet<DepotType> depotTypeRep, IBaseRepositoryGet<WeighingSheets> weighingSheetsRep)
        {
            DepotTypeRep = depotTypeRep;
            WeighingSheetsRep = weighingSheetsRep;
        }
        /// <summary>
        /// 副宰显示 前6条
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(WeighingSheetsShowCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = from a in WeighingSheetsRep.GetAll()
                           join b in DepotTypeRep.GetAll()
                           on a.DepotId equals b.DepotTypeId
                           select new
                           {
                               a.WeighId,
                               b.DepotTypeName,
                               a.ProjectNo,
                               a.WeighDate,
                               GoodsMinuteNo = a.GoodsMinuteId == 1 ? "小肠" : a.GoodsMinuteId == 2 ? "大肠" : a.GoodsMinuteId == 3 ? "猪肚" : a.GoodsMinuteId == 4 ? "猪肝" : "猪心",
                               a.WeighingSheetsNum,
                               a.GrossWeight,
                               a.SkinWeight,
                               a.NetWeight,
                               a.WeighingDesc
                           };
                list = list.OrderBy(x => x.WeighDate).Skip((1 - 1) * 6).Take(6);
                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "成功";
                }
            }
            catch (Exception)
            {
                throw;
            }
            return result;
        }
    }
}
